package com.itheima.springbootmybatislilingguo.controller;

import com.itheima.springbootmybatislilingguo.annotation.MyControllerLog;
import com.itheima.springbootmybatislilingguo.domain.Result;
import com.itheima.springbootmybatislilingguo.domain.Student;
import com.itheima.springbootmybatislilingguo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/add")
    @MyControllerLog(needResponse = false)
    public Result add(@RequestBody Student student) {
        try {
            studentService.add(student);
            return new Result(true, "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增失败");
        }
    }

    @RequestMapping("/deleteById")
    @MyControllerLog(needResponse = false)
    public Result deleteById(Integer id) {
        try {
            studentService.deleteById(id);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    @RequestMapping("/update")
    @MyControllerLog(needResponse = false)
    public Result update(@RequestBody Student student) {
        try {
            studentService.update(student);
            return new Result(true, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "更新失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Student student = studentService.findById(id);
            return new Result(true, "查询成功", student);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Student> studentList = studentService.findAll();
            return new Result(true, "查询成功", studentList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

}
